//
//  KKImageItemsViewController.m
//  KKImagePickerController
//
//  Created by Mac mini on 16/1/21.
//  Copyright © 2016年 JuniorCodingPerson. All rights reserved.
//

#import "KKImageItemsViewController.h"

#import "KKImageLibraryManager.h"

#pragma mark - KKImageItemsViewFooterView methods

@interface KKImageItemsViewFooterView : UICollectionReusableView

@property (nonatomic, strong) UILabel *kk_titleLabel;

@end

@implementation KKImageItemsViewFooterView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: CGRectMake(0, 0, CGRectGetWidth([[UIScreen mainScreen] bounds]), CGRectGetHeight(frame))];
    if (self) {
        
        _kk_titleLabel = [[UILabel alloc] initWithFrame: CGRectMake(0, CGRectGetHeight(self.bounds) - 30, CGRectGetWidth(self.bounds), 20)];
        _kk_titleLabel.textAlignment = NSTextAlignmentCenter;
        _kk_titleLabel.textColor = [UIColor lightGrayColor];
        _kk_titleLabel.font = [UIFont systemFontOfSize: 16];
        _kk_titleLabel.text = @"共200张照片";
        [self addSubview: _kk_titleLabel];
    }
    return self;
}

@end

#pragma mark - KKImageItemsViewController methods

@interface KKImageItemsViewCell : UICollectionViewCell

@property (nonatomic, readonly) UIImageView* itemImageView;

@property (nonatomic, readonly) UIImageView *choosedImageView;

@end

@implementation KKImageItemsViewCell

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame: frame];
    if (self) {
        
        UIView* contentView = [self contentView];
        
        _itemImageView = [[UIImageView alloc] initWithFrame: [self bounds]];
        [contentView addSubview: _itemImageView];
        
        _choosedImageView = [[UIImageView alloc] initWithFrame: CGRectMake(CGRectGetWidth(contentView.bounds) - 28, 4, 24, 24)];
        
        NSString *imagePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"KKImagePickerController.bundle/Images/kk_item_seleted@2x.png"];
        
        _choosedImageView.image = [UIImage imageWithContentsOfFile: imagePath];
        _choosedImageView.hidden = YES;
        [contentView addSubview: _choosedImageView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected {
    [super setSelected: selected];
    _choosedImageView.hidden = !selected;
}

@end

#pragma mark - KKImageItemsViewController methods

static NSString* KKImageItemCollectionCellIdentifier = @"KKImageItemCollectionCellIdentifier";
static NSString* KKImageItemCollectionFooterIdentifier = @"KKImageItemCollectionFooterIdentifier";

@interface KKImageItemsViewController () {
    
    CGSize _itemCellSize;
    
    NSArray *_kk_assetItems;
}

@end

@implementation KKImageItemsViewController

+ (KKImageItemsViewController *)itemsViewController {
    
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    KKImageItemsViewController *viewController = [[KKImageItemsViewController alloc] initWithCollectionViewLayout: flowLayout];
    return viewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat width = CGRectGetWidth(self.collectionView.bounds);
    CGFloat itemWidth = (width - 4 * 5) / 4.0;
    
    _itemCellSize = CGSizeMake(itemWidth, itemWidth);
    
    // Register cell classes
    [self.collectionView registerClass:[KKImageItemsViewCell class] forCellWithReuseIdentifier: KKImageItemCollectionCellIdentifier];
    [self.collectionView registerClass: [KKImageItemsViewFooterView class] forSupplementaryViewOfKind: UICollectionElementKindSectionFooter withReuseIdentifier: KKImageItemCollectionFooterIdentifier];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    [self kk_loadAllItemsData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private methods

- (void)kk_loadAllItemsData {
    
    __weak UICollectionView *weakCollectionView = self.collectionView;
    [[KKImageLibraryManager kk_defaultManager] kk_searchAllItemsFromLibraryWithGroup: _kk_assetsGroup result:^(NSArray *items) {
        _kk_assetItems = [NSArray arrayWithArray: items];
        [weakCollectionView reloadData];
    }];
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _kk_assetItems.count;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return _itemCellSize;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeMake(CGRectGetWidth(collectionView.bounds), 40);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(4, 4, 4, 4);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 4.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 4.0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    KKImageItemsViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: KKImageItemCollectionCellIdentifier forIndexPath: indexPath];
    
    // Configure the cell
    
    ALAsset* assetItem = _kk_assetItems[indexPath.row];
    cell.itemImageView.image = [UIImage imageWithCGImage: assetItem.aspectRatioThumbnail];
    
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionReusableView *footerView = nil;
    
    if ([kind isEqualToString: UICollectionElementKindSectionFooter ]){
        
        footerView = [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier: KKImageItemCollectionFooterIdentifier forIndexPath:indexPath];

    }
    return footerView;
}

@end
